<template>
	<view class="cabinet-add-container">
		<!-- 页面标题 -->
		<view class="page-header">
			<text class="page-title">添加电柜</text>
		</view>

		<scroll-view class="form-container" scroll-y>
			<!-- 第一步：添加电柜信息 -->
			<view class="form-step">
				<view class="step-header">
					<view class="step-number">1</view>
					<text class="step-title">添加电柜信息</text>
				</view>

				<view class="form-section">
					<!-- 二维码编号 -->
					<view class="form-item required">
						<text class="form-label">二维码编号</text>
						<view class="input-with-action">
							<input class="form-input" placeholder="输入二维码编号" v-model="formData.cabinetSn"
								maxlength="50" />
							<button class="action-btn" @click="handleScan">点击扫一扫</button>
						</view>
					</view>

					<!-- 仓门数 -->
					<view class="form-item required">
						<text class="form-label">仓门数</text>
						<input class="form-input" placeholder="输入电柜仓门数" v-model="formData.boxNum" type="number"
							maxlength="3" />
					</view>

					<!-- 电柜名称 -->
					<view class="form-item required">
						<text class="form-label">电柜名称</text>
						<input class="form-input" placeholder="设置电柜名称" v-model="formData.cabinetName" maxlength="50" />
					</view>

					<!-- 电柜照片 -->
					<view class="form-item required">
						<text class="form-label">电柜照片</text>
						<view class="upload-section">
							<view class="upload-tips">
								<text class="tip-text">上传jpg/png文件，不超过2m</text>
							</view>
							<view class="upload-box" @click="uploadImage">
								<view class="upload-icon">
									<image v-if="formData.picture" :src="formData.picture" mode="widthFix" style="width: 100px;"></image>
									<text class="icon" v-else>📷</text>
									
								</view>
								<text class="upload-text">上传图片</text>
								<text class="upload-filename" v-if="formData.picture">{{formData.imageName}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>


			<!-- 第二步：将电柜添加至门店 -->
			<view class="form-step">
				<view class="step-header">
					<view class="step-number">2</view>
					<text class="step-title">电柜添加</text>
				</view>

				<view class="form-section">

					<!-- 柜子地址 -->
					<view class="form-item required">
						<text class="form-label">柜子地址</text>
							<view class="picker-content">
								<input class="form-input half" placeholder="电柜地址" v-model="formData.address" />
								<!-- <text class="picker-arrow">▼</text> -->
								<view class="location-btn" @click="getCurrentLocation">
									<text class="location-icon">📍</text>
								</view>
							</view>
					</view>



					<!-- 柜子经纬度 -->
					<view class="form-item">
						<text class="form-label">柜子经纬度</text>
						<view class="input-group">
							<input class="form-input half" placeholder="经度" v-model="formData.lng" />
							<input class="form-input half" placeholder="纬度" v-model="formData.lat" />
						</view>
					<!-- 	<view class="location-action">
							<button class="location-btn" @click="getCurrentLocation">
								<text class="location-icon">📍</text>
								<text class="location-text">获取当前位置</text>
							</button>
						</view> -->
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 底部确认按钮 -->
		<view class="action-bar">
			<button class="confirm-btn" @click="handleConfirm">确认添加</button>
		</view>
	</view>
</template>

<script>
	import api from '@/config/api.js'
	export default {
		data() {
			return {
				formData: {
					cabinetSn: '',
					boxNum: '',
					cabinetName: '',
					picture: '',
					imageName:'',
					address: '',
					lng: '',
					lat: ''
				}
			}
		},
		methods: {
			handleScan() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: (res) => {
						console.log('扫码结果:', res)
						const snMatch = res.result.match(/sn=([^&]+)/) || res.result.match(/cabinetSn=([^&]+)/);
						const cabinetSn = snMatch ? snMatch[1] : null;
						this.formData.cabinetSn = cabinetSn
						uni.showToast({
							title: '扫码成功',
							icon: 'success'
						})
					},
					fail: (err) => {
						console.error('扫码失败:', err)
						uni.showToast({
							title: '扫码失败',
							icon: 'none'
						})
					}
				})
			},

			uploadImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success:async (res)=>{
						const tempFilePaths = res.tempFilePaths
						const file = res.tempFiles[0]

						// 检查文件大小
						if (file.size > 2 * 1024 * 1024) {
							uni.showToast({
								title: '图片大小不能超过2M',
								icon: 'none'
							})
							return
						}
						console.log('file',file)
						
						
						api.fix.uploadFile({file},tempFilePaths[0]).then((upload)=>{
							uni.showToast({
								title: '图片已上传',
								icon: 'success'
							})
							console.log('upload',upload)
							this.formData.picture = upload.url
							this.formData.imageName = upload.name || '电柜图片'
						})
						
						
					},
					fail: (error) => {
						console.error('选择图片失败:', error)
					}
				})
			},

			onAddressChange(e) {
				const value = e.detail.value
				this.formData.address = value.join(' ')
			},

			getCurrentLocation() {
				uni.showLoading({
					title: '定位中...',
					mask: true
				})

				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						this.formData.lng = res.longitude.toFixed(6)
						this.formData.lat = res.latitude.toFixed(6)

						uni.hideLoading()
						uni.showToast({
							title: '定位成功',
							icon: 'success'
						})
					},
					fail: (error) => {
						uni.hideLoading()
						console.error('获取位置失败:', error)
						uni.showToast({
							title: '定位失败，请手动输入',
							icon: 'none'
						})
					}
				})
			},

			validateForm() {
				if (!this.formData.cabinetSn) {
					uni.showToast({
						title: '请输入二维码编号',
						icon: 'none'
					})
					return false
				}
				if (!this.formData.boxNum) {
					uni.showToast({
						title: '请输入仓门数',
						icon: 'none'
					})
					return false
				}
				if (!this.formData.cabinetName) {
					uni.showToast({
						title: '请输入电柜名称',
						icon: 'none'
					})
					return false
				}
				if (!this.formData.picture) {
					uni.showToast({
						title: '请上传电柜照片',
						icon: 'none'
					})
					return false
				}
				if (!this.formData.address) {
					uni.showToast({
						title: '请选择电柜地址',
						icon: 'none'
					})
					return false
				}
				return true
			},

			async handleConfirm() {
				if (!this.validateForm()) {
					return
				}

				try {
					uni.showLoading({
						title: '添加中...',
						mask: true
					})

					// 构建提交数据
					const submitData = {
						...this.formData
					}

					const result = await api.cabinet.addCabinet(submitData)

					uni.showToast({
						title: '添加成功',
						icon: 'success'
					})

					// 返回电柜列表页
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)

				} catch (error) {
					console.error('添加电柜失败:', error)
					uni.showToast({
						title: '添加失败',
						icon: 'none'
					})
				} finally {
					uni.hideLoading()
				}
			}
		}
	}
</script>

<style scoped>
	.cabinet-add-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	/* 页面标题 */
	.page-header {
		background-color: #fff;
		padding: 30rpx;
		border-bottom: 1rpx solid #eee;
	}

	.page-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	/* 表单容器 */
	.form-container {
		height: calc(100vh - 200rpx);
		padding: 20rpx;
		box-sizing: border-box;
	}

	/* 步骤头部 */
	.form-step {
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.step-header {
		display: flex;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
		background-color: #fafafa;
	}

	.step-number {
		width: 40rpx;
		height: 40rpx;
		background-color: #1890ff;
		color: #fff;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		font-weight: bold;
		margin-right: 20rpx;
	}

	.step-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	/* 表单区域 */
	.form-section {
		padding: 30rpx;
	}

	/* 表单项 */
	.form-item {
		margin-bottom: 40rpx;
	}

	.form-item.required .form-label::before {
		content: '*';
		color: #f5222d;
		margin-right: 8rpx;
	}

	.form-label {
		display: block;
		font-size: 28rpx;
		font-weight: 500;
		color: #333;
		margin-bottom: 16rpx;
	}

	.form-input {
		width: 100%;
		height: 80rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #333;
		border: 1rpx solid #e8e8e8;
	}

	.form-input.half {
		width: calc(50% - 10rpx);
	}

	.input-group {
		display: flex;
		gap: 20rpx;
	}

	/* 输入框带操作按钮 */
	.input-with-action {
		display: flex;
		gap: 20rpx;
	}

	.input-with-action .form-input {
		flex: 1;
	}

	.input-with-action .action-btn {
		width: 200rpx;
		height: 80rpx;
		background-color: #f5f5f5;
		color: #1890ff;
		border: 1rpx solid #1890ff;
		border-radius: 8rpx;
		font-size: 24rpx;
		line-height: 80rpx;
		white-space: nowrap;
	}

	/* 上传区域 */
	.upload-section {
		border: 1rpx solid #e8e8e8;
		border-radius: 8rpx;
		overflow: hidden;
	}

	.upload-tips {
		padding: 16rpx 20rpx;
		background-color: #fafafa;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.tip-text {
		font-size: 24rpx;
		color: #999;
	}

	.upload-box {
		padding: 40rpx 20rpx;
		text-align: center;
		background-color: #f5f5f5;
	}

	.upload-icon {
		font-size: 60rpx;
		margin-bottom: 16rpx;
	}

	.upload-text {
		display: block;
		font-size: 28rpx;
		color: #666;
		margin-bottom: 8rpx;
	}

	.upload-filename {
		display: block;
		font-size: 24rpx;
		color: #1890ff;
		word-break: break-all;
	}

	/* 信息区域 */
	.info-section {
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		padding: 30rpx 30rpx 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
		background-color: #fafafa;
	}

	/* 开关组 */
	.switch-group {
		display: flex;
		align-items: center;
		height: 80rpx;
	}

	.switch-label {
		font-size: 28rpx;
		color: #333;
		margin-left: 20rpx;
	}

	/* 选择器 */
	.form-picker {
		width: 100%;
	}

	.picker-content {
		width: 100%;
		height: 80rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border: 1rpx solid #e8e8e8;
	}

	.picker-text {
		font-size: 28rpx;
		color: #333;
	}

	.picker-text.placeholder {
		color: #999;
	}

	.picker-arrow {
		font-size: 24rpx;
		color: #999;
	}

	/* 定位操作 */
	.location-action {
		margin-top: 20rpx;
	}

	.location-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f5f5f5;
		color: #1890ff;
		border: 1rpx solid #1890ff;
		border-radius: 8rpx;
		height: 60rpx;
		padding: 0 20rpx;
		font-size: 24rpx;
	}

	.location-icon {
		margin-right: 8rpx;
		font-size: 20rpx;
	}

	/* 底部操作栏 */
	.action-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		padding: 20rpx 30rpx;
		border-top: 1rpx solid #eee;
		box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.06);
	}

	.confirm-btn {
		background-color: #1890ff;
		color: #fff;
		height: 80rpx;
		border-radius: 40rpx;
		font-size: 32rpx;
		font-weight: bold;
		line-height: 80rpx;
	}
</style>